<template>
    <div class="box">
      <el-container>
        <el-header>
          <h2 class="title">规则管理</h2>
        </el-header>
        <el-main>
          <!-- 显示当前规则 -->
          <el-card>
            <el-row>
              <el-col :span="24">
                <el-form :model="form" label-width="100px">
                  <el-form-item label="规则内容">
                    <el-input 
                      type="textarea" 
                      v-model="form.rules" 
                      :rows="20" 
                      placeholder="请输入规则" 
                    />
                  </el-form-item>
                </el-form>
              </el-col>
            </el-row>
  
            <el-row>
              <el-col :span="24">
                <el-button type="primary" @click="updateRules">更新规则</el-button>
              </el-col>
            </el-row>
          </el-card>
        </el-main>
      </el-container>
    </div>
  </template>
  
  <script>
    import api from "@/utils/api";
    import { Message } from "element-ui";
  
  export default {
    data() {
      return {
        form: {
          rules: '', // 用来存储规则内容
        },
      };
    },
    created() {
      this.fetchRules();
    },
    mounted() {
        this.fetchRules();
    },
    methods: {
      // 获取当前规则
      fetchRules() {
        api.queryRule().then((data) => {
            if (data) {
                this.form.rules = data.data;
            } else {
                Message.error("获取规则失败！")
            }
        });
      },
      // 更新规则
      updateRules() {
        console.log("rules is ", this.form.rules)
        api.updateRule(this.form.rules).then((data) => {
            if (data) {
                Message.success("规则更新成功");
            } else {
                Message.error("更新规则失败");
            }
        });
        this.fetchRules();
      },
    },
  };
  </script>
  
  <style>
  .box {
    height: 100%;
    width: 100%;
    padding: 20px;
    background-color: #f9f9f9;
    position: relative;
  }
  #app {
    width: 100%;
    height: 100%;
    font-family: Avenir, Helvetica, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-align: center;
    color: #2c3e50;
    margin-top: 60px;
  }
  
  .el-header {
    text-align: center;
    font-size: 24px;
  }
  .title {
    text-align: left;
    font-size: 24px;
    font-weight: bold;
    color: #333;
    margin-bottom: 20px;
}

  </style>